package com.njit.farm.service.impl;

import com.njit.farm.dao.FOrderItemMapper;
import com.njit.farm.dao.FOrderMapper;
import com.njit.farm.dataobject.FGoods;
import com.njit.farm.dataobject.FOrder;
import com.njit.farm.dataobject.FOrderItem;
import com.njit.farm.model.Goods;
import com.njit.farm.model.Order;
import com.njit.farm.model.OrderItem;
import com.njit.farm.model.Result;
import com.njit.farm.service.GoodsService;
import com.njit.farm.service.OrderService;
import com.njit.farm.util.IDUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;



@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    FOrderItemMapper orderItemMapper;

    @Resource
    FOrderMapper fOrderMapper;

    @Resource
    GoodsService goodsService;

    @Override
    public Result addOrderItem(OrderItem orderItem) {

        Result result = new Result();

        FOrderItem fOrderItem = new FOrderItem();
        fOrderItem.setOiId(IDUtils.uuid());
        fOrderItem.setOiAmount(orderItem.getAmount());
        fOrderItem.setoId(orderItem.getoId());
        fOrderItem.setgId(orderItem.getGoods().getId());

        orderItemMapper.insert(fOrderItem);

        updateOrderTotal(orderItem.getoId());

        result.setSucc(true);

        return result;
    }


    @Override
    public Result updateOrderItem(OrderItem orderItem) {

        Result result = new Result();

        FOrderItem fOrderItem = new FOrderItem();
        fOrderItem.setOiId(orderItem.getId());
        fOrderItem.setOiAmount(orderItem.getAmount());

        orderItemMapper.updateByPrimaryKey(fOrderItem);
        FOrderItem fOrderItem1 = orderItemMapper.selectByPrimaryKey(orderItem.getId());

        updateOrderTotal(fOrderItem1.getoId());

        result.setSucc(true);
        return result;
    }

    @Override
    public Result deleteOrderItem(String oiId) {

        Result result = new Result();

        FOrderItem fOrderItem = orderItemMapper.selectByPrimaryKey(oiId);
        orderItemMapper.deleteByPrimaryKey(oiId);


        updateOrderTotal(fOrderItem.getoId());

        result.setSucc(true);

        return result;
    }

    @Override
    public Result add(Order order) {

        Result result = new Result();


        FOrder fOrder = new FOrder();
        fOrder.setoId(IDUtils.uuid());
        fOrder.setoState(order.getState());
        fOrder.setoTotal(0f);
        fOrder.setuId(order.getuId());
        fOrder.setoCreateTime(new Date());

        fOrderMapper.insert(fOrder);

        //将相关数据返回
        Order resOrder = new Order();
        resOrder.setId(fOrder.getoId());
        resOrder.setTotal(fOrder.getoTotal());
        resOrder.setState(fOrder.getoState());
        resOrder.setCreateTime(fOrder.getoCreateTime());

        result.setSucc(true);
        result.setData(resOrder);
        return result;
    }

    @Override
    public Result update(Order order) {

        Result result = new Result();

        FOrder fOrder = new FOrder();
        fOrder.setoState(order.getState());
        fOrder.setuId(order.getuId());

        fOrderMapper.updateByPrimaryKey(fOrder);

        return result;
    }


    private Result updateOrderTotal(String oId) {

        Result result = new Result();

        fOrderMapper.updateTotal(oId);

        result.setSucc(true);
        return result;

    }

    @Override
    public Result queryByUserId(String uid) {

        Result result = new Result();

        List<Order> orders = fOrderMapper.selectOrdersByUserId(uid);

        result.setSucc(true);
        result.setData(orders);

        return result;
    }

    @Override
    public Result remove(String oid) {

        Result result = new Result();

        fOrderMapper.deleteByPrimaryKey(oid);

        result.setSucc(true);
        return result;
    }
}
